﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfControlLibrary.MoveItems
{
    /// <summary>
    /// Grid.xaml 的交互逻辑
    /// </summary>
    public partial class Grid : UserControl
    {
        public Grid()
        {
            InitializeComponent();
        }



        public int Count
        {
            get { return (int)GetValue(CountProperty); }
            set { SetValue(CountProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Count.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CountProperty =
            DependencyProperty.Register("Count", typeof(int), typeof(Grid), new PropertyMetadata(0));



        public double WidthTotal
        {
            get { return (double)GetValue(WidthTotalProperty); }
            set { SetValue(WidthTotalProperty, value); Ini(); }
        }

        // Using a DependencyProperty as the backing store for WidthTotal.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty WidthTotalProperty =
            DependencyProperty.Register("WidthTotal", typeof(double), typeof(Grid), new PropertyMetadata(0d));



        public double HightTotal
        {
            get { return (double)GetValue(HightTotalProperty); }
            set { SetValue(HightTotalProperty, value); Ini(); }
        }

        // Using a DependencyProperty as the backing store for HightTotal.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HightTotalProperty =
            DependencyProperty.Register("HightTotal", typeof(double), typeof(Grid), new PropertyMetadata(0d));




        public double ItemHigh
        {
            get { return (double)GetValue(ItemHighProperty); }
            set { SetValue(ItemHighProperty, value); Ini(); }
        }

        // Using a DependencyProperty as the backing store for ItemHigh.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemHighProperty =
            DependencyProperty.Register("ItemHigh", typeof(double), typeof(Grid), new PropertyMetadata(0d));



        public bool IsAutoMove
        {
            get { return (bool)GetValue(IsAutoMoveProperty); }
            set { SetValue(IsAutoMoveProperty, value); Ini(); }
        }

        // Using a DependencyProperty as the backing store for IsAutoMove.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsAutoMoveProperty =
            DependencyProperty.Register("IsAutoMove", typeof(bool), typeof(Grid), new PropertyMetadata(false));


        bool BinIni = false;
        private void Ini()
        {
            if (!BinIni && Count != 0 && HightTotal != 0 && WidthTotal != 0 && ItemHigh != 0)
            {
                BinIni = true;

                var itemWidth = WidthTotal/Count;

                for (int i = 0; i < Count; i++)
                {
                    var item = new WpfControlLibrary.MoveItems.Item();
                    item.Name = $"我是{i}";
                    item.Width= itemWidth;
                    item.ItemHight = ItemHigh;
                    item.BackgroundHight = HightTotal;

                    this.Grid_Fill_Content.Children.Add(item);
                }
            }

            if (BinIni)
            {
                if (this.Grid_Fill_Content.Children.Count > 0)
                {
                    for (int i = 0; i < this.Grid_Fill_Content.Children.Count; i++)
                    {
                        var t = (WpfControlLibrary.MoveItems.Item)this.Grid_Fill_Content.Children[i];
                        t.IsAutoMove = IsAutoMove;
                    }
                }
            }
        }
    }
}
